package com.example.mykotlinapp

import java.lang.IllegalArgumentException

/**
 *
 *
 * @author zhudeshuai
 * @since 2019/7/1
 */

//复习，集合遍历

private fun test40() {
    val list = arrayListOf("java", "c++", "c#", "kotlin", "js", "swift", "oc")
    for ((index, item) in list.withIndex()) {
        println("index:$index,value:$item")
    }
    list.forEach {
        println(it)
    }
}

//正则表达式和三重引号的字符串
private fun test41(path: String) {
    val directory = path.substringBeforeLast("/")
    val fullName = path.substringAfterLast("/")
    val fileName = fullName.substringBeforeLast(".")
    val extension = fullName.substringAfterLast(".")

    println("dir:$directory,fullName:$fullName,fileName:$fileName,extension:$extension")
}

//局部函数和扩展
/*class User(val userId: Int, val userName: String)

private fun test42(user: User) {

    //可以继续优化为User的扩展函数
    private fun checkValied(value: String, valueName: String) {
        if (value.isEmpty()) {
            throw IllegalArgumentException("the $valueName can't be empty")
        }
    }

    checkValied(user.userName, "userName")
}*/


//接口定义
interface Clickable {
    fun click()
}

//实现接口
class Button : Clickable {
    override fun click() = println("I was clicked")

}


//kotlin中默认创建的类是不可以被继承的(只有open修饰后才可以被继承)

open class View(width: Int, height: Int)

class TextView(width: Int, height: Int) : View(width, height) {

}

//抽象类
abstract class Animated {
    abstract fun animate()
    private fun stopAnimating() {
    }

    private fun animateTwice() {

    }
}


//可见性修饰符，kotlin中默认是public   类的扩展函数不能访问private和protected成员
internal open class TalkativeButton {
    private fun yell() = println("Hey!")
    protected fun whisper() = println("let's talk")
}

/*
private fun TalkativeButton.giveSpeech() {//无法访问internal的类，以为改函数默认为public
    yell()
    whisper()
}
*/




private fun main() {
    test41("/Users/yole/kotlin-book/chapter.doc")
}